DECLARE @marc_no VARCHAR(50)
DECLARE @b_num int
DECLARE @b_num_now int

DECLARE db_cursor CURSOR FOR 
select marc_no from bookinfo where marc_no = 9300049055

OPEN db_cursor  
FETCH NEXT FROM db_cursor INTO @marc_no  

WHILE @@FETCH_STATUS = 0  
BEGIN   
		set @b_num = (select b_num from bookinfo where marc_no = @marc_no)
		set @b_num_now = (select count(*) from bookcoll where marc_no = @marc_no)
		set @b_num = @b_num - @b_num_now
		IF @b_num > 0
			BEGIN
				WHILE (@b_num > 0)
				BEGIN
    				insert into bookcoll(nd_no, marc_no, b_sts, f_no, f_row, f_col, op_date) select top 1 nd_no, marc_no, b_sts, f_no, f_row, f_col, getdate() from bookcoll where marc_no = @marc_no
    				SET @b_num = @b_num - 1
				END
			END
		ELSE
			BEGIN			
				WHILE (@b_num > 0)
				BEGIN
    				insert into bookcoll(nd_no, marc_no, b_sts, f_no, f_row, f_col, op_date) select 930, (select marc_no from bookinfo a where a.imp_id = b.imp_id), '在馆', (select f_no from frame c where c.f_name = b.f_name),f_row, f_col, getdate()from book_import b
    				SET @b_num = @b_num - 1
				END			
			END
		
      FETCH NEXT FROM db_cursor INTO @marc_no 
END 

CLOSE db_cursor
DEALLOCATE db_cursor